Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CT-520] Validatebasic for new MsgBatchCancel #1101

Merged
merged 5 commits into from
Feb 27, 2024
Merged

Conversation

jonfung-dydx
Copy link
Contributor

@jonfung-dydx jonfung-dydx commented Feb 27, 2024

MsgBatchCancel stateless validation.

Copy link

linear bot commented Feb 27, 2024

Copy link
Contributor

coderabbitai bot commented Feb 27, 2024

Walkthrough

The update introduces a new message type, MsgBatchCancel, designed for batch cancellation operations within the clob module. This enhancement includes mechanisms for creating, validating, and processing batch cancellation requests. Specifically, it ensures that such requests are correctly formed, focusing on the validation of subaccount IDs, order batch sizes, and the uniqueness of client IDs in each batch. This addition aims to streamline and secure the process of batch cancellations.

Changes

File Change Summary
.../x/clob/types/message_batch_cancel.go Introduced MsgBatchCancel for batch cancellation operations, with functions for creation and validation.
.../x/clob/types/constants.go Added MaxMsgBatchCancelBatchSize constant with a value of 100 for MsgBatchCancel maximum cancels per message.
.../x/clob/types/errors.go Added ErrInvalidBatchCancel error with code 45 and message "Invalid batch cancel message" for time in force validation.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@jonfung-dydx jonfung-dydx requested a review from jayy04 February 27, 2024 05:23
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 37cf36d and 8f77dbe.
Files selected for processing (2)
  • protocol/x/clob/types/message_batch_cancel.go (1 hunks)
  • protocol/x/clob/types/message_batch_cancel_test.go (1 hunks)

Comment on lines 12 to 18
// NewMsgCancelOrderShortTerm constructs a MsgBatchCancel from an `OrderId` and a `GoodTilBlock`.
func NewMsgBatchCancel(subaccountId types.SubaccountId, cancelBatch []OrderBatch, goodTilBlock uint32) *MsgBatchCancel {
return &MsgBatchCancel{
SubaccountId: subaccountId,
ShortTermCancels: cancelBatch,
GoodTilBlock: goodTilBlock,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor NewMsgBatchCancel is well-defined, correctly initializing a MsgBatchCancel instance. However, the parameter name cancelBatch might be misleading as it suggests a single batch, whereas it represents multiple batches. Consider renaming it to cancelBatches for clarity.

- func NewMsgBatchCancel(subaccountId types.SubaccountId, cancelBatch []OrderBatch, goodTilBlock uint32) *MsgBatchCancel {
+ func NewMsgBatchCancel(subaccountId types.SubaccountId, cancelBatches []OrderBatch, goodTilBlock uint32) *MsgBatchCancel {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// NewMsgCancelOrderShortTerm constructs a MsgBatchCancel from an `OrderId` and a `GoodTilBlock`.
func NewMsgBatchCancel(subaccountId types.SubaccountId, cancelBatch []OrderBatch, goodTilBlock uint32) *MsgBatchCancel {
return &MsgBatchCancel{
SubaccountId: subaccountId,
ShortTermCancels: cancelBatch,
GoodTilBlock: goodTilBlock,
}
// NewMsgCancelOrderShortTerm constructs a MsgBatchCancel from an `OrderId` and a `GoodTilBlock`.
func NewMsgBatchCancel(subaccountId types.SubaccountId, cancelBatches []OrderBatch, goodTilBlock uint32) *MsgBatchCancel {
return &MsgBatchCancel{
SubaccountId: subaccountId,
ShortTermCancels: cancelBatch,
GoodTilBlock: goodTilBlock,
}

Comment on lines 21 to 53
// ValidateBasic performs stateless validation for
func (msg *MsgBatchCancel) ValidateBasic() (err error) {
subaccountId := msg.GetSubaccountId()
if err := subaccountId.Validate(); err != nil {
return err
}

cancelBatches := msg.GetShortTermCancels()
totalNumberCancels := 0
for _, cancelBatch := range cancelBatches {
totalNumberCancels += len(cancelBatch.GetClientIds())
seenClientIds := map[uint32]struct{}{}
for _, clientId := range cancelBatch.GetClientIds() {
if _, seen := seenClientIds[clientId]; seen {
return errorsmod.Wrapf(
ErrInvalidBatchCancel,
"Batch cancel cannot have duplicate cancels. Duplicate clob pair id: %+v, client id: %+v",
cancelBatch.GetClobPairId(),
clientId,
)
}
seenClientIds[clientId] = struct{}{}
}
}
if uint32(totalNumberCancels) > MaxMsgBatchCancelBatchSize {
return errorsmod.Wrapf(
ErrInvalidBatchCancel,
"Batch cancel cannot have over %+v orders. Order count: %+v",
MaxMsgBatchCancelBatchSize,
totalNumberCancels,
)
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ValidateBasic method performs several checks, including subaccount ID validation and ensuring no duplicate client IDs within each batch. However, there are a few areas for improvement:

  1. The error message for duplicate client IDs (lines 35-40) could be enhanced by specifying it's within a single batch for clarity.
  2. The check for the total number of cancels exceeding MaxMsgBatchCancelBatchSize (lines 45-51) is crucial. Ensure MaxMsgBatchCancelBatchSize is defined and documented elsewhere in the codebase, as it's a key constraint.
  3. Consider adding a comment above the loop starting at line 30 to explain the purpose of the loop, which is to ensure no duplicate client IDs within each batch and to count the total number of cancels.

Overall, the method is logically sound but could benefit from minor refinements for clarity and maintainability.

Comment on lines 12 to 122
msg: *types.NewMsgBatchCancel(
constants.InvalidSubaccountIdNumber,
[]types.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{
0, 1, 2, 3,
},
},
},
10,
),
err: satypes.ErrInvalidSubaccountIdNumber,
},
"over 100 cancels in for one clob pair id": {
msg: *types.NewMsgBatchCancel(
constants.Alice_Num0,
[]types.OrderBatch{
{
ClobPairId: 0,
ClientIds: oneOverMax,
},
},
10,
),
err: types.ErrInvalidBatchCancel,
},
"over 100 cancels split over two clob pair id": {
msg: *types.NewMsgBatchCancel(
constants.Alice_Num0,
[]types.OrderBatch{
{
ClobPairId: 0,
ClientIds: oneOverMax[:52],
},
{
ClobPairId: 1,
ClientIds: oneOverMax[:52],
},
},
10,
),
err: types.ErrInvalidBatchCancel,
},
"success: two clob pair id, 100 cancels": {
msg: *types.NewMsgBatchCancel(
constants.Alice_Num0,
[]types.OrderBatch{
{
ClobPairId: 0,
ClientIds: oneOverMax[:50],
},
{
ClobPairId: 1,
ClientIds: oneOverMax[:50],
},
},
10,
),
err: nil,
},
"success: one clob pair id, 100 cancels": {
msg: *types.NewMsgBatchCancel(
constants.Alice_Num0,
[]types.OrderBatch{
{
ClobPairId: 0,
ClientIds: oneOverMax[:types.MaxMsgBatchCancelBatchSize],
},
},
10,
),
err: nil,
},
"duplicate clob pair ids": {
msg: *types.NewMsgBatchCancel(
constants.Alice_Num0,
[]types.OrderBatch{
{
ClobPairId: 0,
ClientIds: []uint32{
0, 1, 2, 3, 1,
},
},
},
10,
),
err: types.ErrInvalidBatchCancel,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
return
}
require.NoError(t, err)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases in TestMsgBatchCancel_ValidateBasic are comprehensive, covering a wide range of scenarios. A few suggestions for improvement:

  1. The setup for oneOverMax (lines 13-16) is clear, but consider adding a brief comment explaining its purpose, which is to create a client ID slice exceeding the maximum allowed size.
  2. In the test case names, such as "over 100 cancels in for one clob pair id" (line 37), ensure consistency in describing the test scenarios. For example, if MaxMsgBatchCancelBatchSize is not always 100, use a more generic description or reference the constant directly.
  3. The use of constants.Alice_Num0 and constants.InvalidSubaccountIdNumber (lines 24, 39, etc.) is good for readability, but ensure these constants are well-documented in their definition to avoid confusion.

Overall, the tests are well-structured and effectively validate the functionality of MsgBatchCancel. Minor improvements in documentation and naming could enhance readability and maintainability.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f77dbe and 040422a.
Files selected for processing (3)
  • protocol/x/clob/types/constants.go (1 hunks)
  • protocol/x/clob/types/errors.go (1 hunks)
  • protocol/x/clob/types/message_batch_cancel.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/clob/types/message_batch_cancel.go
Additional comments: 2
protocol/x/clob/types/constants.go (1)
  • 11-13: The addition of MaxMsgBatchCancelBatchSize with a value of 100 is a good practice for defining system limits. Ensure this value aligns with the system's capacity and the expected use cases for batch cancellation.
protocol/x/clob/types/errors.go (1)
  • 209-213: The introduction of ErrInvalidBatchCancel is a positive step towards robust error handling for the new MsgBatchCancel functionality. Verify the uniqueness of the error code within the module. Consider enhancing the error message to provide more detail about common validation failures if applicable.

@@ -8,6 +8,10 @@ import (
// `MsgPlaceOrder` or `MsgCancelOrder` message will be considered valid by the validator.
const ShortBlockWindow uint32 = 20

// MaxMsgBatchCancelBatchSize represents the maximum number of cancels that a MsgBatchCancel
// can have in one Msg.
const MaxMsgBatchCancelBatchSize uint32 = 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for my understanding - did we decide to set to to 100 or 1000?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// ValidateBasic performs stateless validation for the `MsgBatchCancel` msg.
func (msg *MsgBatchCancel) ValidateBasic() (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so ValidateBasic is actually optional as of v0.50 - I think it's still supported for backwards compatibility but can you maybe spend 10-15 minutes to see how it works w/ the new version and what was the motivation? kind of curious about this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering if transactions that are obviously wrong (e.g. failing ValidateBasic) can now enter mempool and be included in a block

Copy link
Contributor Author

@jonfung-dydx jonfung-dydx Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosmos/cosmos-sdk#15648

Seems like they have moved all validation to the msg server.

Example PRS
cosmos/cosmos-sdk#15832

Based on this comment
https://github.com/cosmos/cosmos-sdk/pull/15782/files#r1163771773
it seems like we lose the functionality of verifying if the proposal message is correct before execution. Apparently simulation covers that case you have mentioned. However, a bit confused as users can still submit messages without simulating first.

10,
),
err: types.ErrInvalidBatchCancel,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add cases where len(short_term_cancel) == 0 and len(client_ids) == 0?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 040422a and cad3af4.
Files selected for processing (3)
  • protocol/x/clob/types/errors.go (1 hunks)
  • protocol/x/clob/types/message_batch_cancel.go (1 hunks)
  • protocol/x/clob/types/message_batch_cancel_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/x/clob/types/errors.go
  • protocol/x/clob/types/message_batch_cancel.go
  • protocol/x/clob/types/message_batch_cancel_test.go

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cad3af4 and dc9ee99.
Files selected for processing (1)
  • protocol/x/clob/types/message_batch_cancel.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/clob/types/message_batch_cancel.go

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dc9ee99 and a0d1b23.
Files selected for processing (1)
  • protocol/x/clob/types/message_batch_cancel.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/clob/types/message_batch_cancel.go

@jonfung-dydx jonfung-dydx merged commit 9bf9875 into main Feb 27, 2024
17 checks passed
@jonfung-dydx jonfung-dydx deleted the jonfung/validateBasic branch February 27, 2024 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants